define(["angularAMD"], function (angularAMD) {
    angularAMD.service('LoadModuleService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            var service = this;

            // model_no : 1(正向追溯)、2(生產追溯)
            service.init = function(model_no, callbackDashboardPanel){
                var content = {
                    model_no: model_no
                };
                loadModeInfoGet(content,function(data){
                    openDialog(model_no, data, callbackDashboardPanel);
                });
            };

            function openDialog(model_no, modeData, callbackDashboardPanel){
                $mdDialog.dialog(
                    'module/moduleQT/program/service/LoadModule.tmp.html',
                    function (dialog) {
                        return {
                            loadMode: modeData.LoadMode,
                            css_url:'module/moduleQT/program/service/LoadModule.css',
                            back:function(){
                                dialog.hide();
                            },
                            getDashboardPanel: function(item){
                                callbackDashboardPanel(item.model_setting);
                                dialog.hide();
                            },
                            deleteMode: function(item){
                                $rootScope.showConfirm($filter('translate')('QT_standard.common.loadModule.msg.whether_delete_this_module'), function (confirmDialog) {
                                    var content = {
                                        model_no : model_no,
                                        model_name: item.model_name
                                    };
                                    deleteModeProcess(content,function(data){
                                        if(data.code == '1'){
                                            $rootScope.showToast($filter('translate')('QT_standard.common.loadModule.msg.delete_success'));
                                            dialog.loadMode.splice(dialog.loadMode.indexOf(item), 1);
                                            confirmDialog.hide();
                                        } else {
                                            $rootScope.showAlert($filter('translate')('QT_standard.common.loadModule.msg.delete_fail'));
                                        };       
                                    });
                                }); 
                            }
                        };
                    }
                );
            };

            function loadModeInfoGet(content, callback){
                // var data = {
                //     LoadMode : [
                //         {
                //             model_name: '模版1',
                //             model_setting: ['machine_details','workreport_details','inspect_details','plotno_details']
                //         },
                //         {
                //             model_name: '模版2',
                //             model_setting: ['workreport_details','materialopt_details','inspect_details','machine_details']
                //         },
                //         {
                //             model_name: '模版3',
                //             model_setting: ['plotno_details','inspect_details','materialopt_details','machine_details']
                //         }
                //     ]
                // }
                // if(callback)
                //     callback(data);

                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.template_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 刪除模版服務
            function deleteModeProcess(content, callback){
                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.del_template_process',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

        }
    ])
});
